package com.lr.server.xml;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class PersonHandler extends DefaultHandler{
	
	private List<Person> list;
	
	private Person temp;
	
	private String tag;

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		System.out.println("开始解析文档");
		list = new ArrayList<Person>();
	}

	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
		System.out.println("解析文档结束");
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		System.out.println("开始解析文档节点"+qName);
		if(null != qName){
			tag = qName;
		}	
		if(qName.equals("person")){
			temp = new Person();
		}
			
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
		System.out.println("结束解析文档节点"+qName);
		if(qName != null && qName.equals("person")){
			list.add(temp);
		}
		tag = null;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		System.out.println("解析文档节点"+new String(ch,start,length));
		if(null != tag && tag.equals("name")){
			temp.setName(new String(ch,start,length));
		}
		else if(null != tag && tag.equals("age")){
			temp.setAge(new String(ch,start,length));
		}
	}

}
